package factory;

import java.util.*;
import java.sql.*;

import classes.cards.ICard;
import classes.cards.InfectionCityCard;
import classes.cards.PlayerCityCard;
import classes.city.City;
import classes.deck.InfectionDeck;
import classes.deck.PlayerDeck;
import classes.diseases.Disease;
import classes.diseases.MainDisease;

public class Factory {

	private static HashMap<String, Disease> diseases = new HashMap<String, Disease>();
	private static HashMap<String, City> cities = new HashMap<String, City>();

	public static HashMap<String, Disease> createDisease() {

		Connection con = null;

		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
			con = DriverManager
					.getConnection("jdbc:derby:C:\\Users\\Anton\\Desktop\\Imperial Camp\\PandemicDB;create=true; user = pandemic; password = pandemic");

			PreparedStatement selectFromDisease = con
					.prepareStatement("select * from factoryData.disease");
			ResultSet res = selectFromDisease.executeQuery();

			while (res.next()) {
				String name = res.getString("name");
				String color = res.getString("color");

				diseases.put(name, new MainDisease(name, color));

			}

		} catch (InstantiationException | IllegalAccessException
				| ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return diseases;
	}

	public static HashMap<String, City> createCities() {

		Connection con = null;

		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
			con = DriverManager
					.getConnection("jdbc:derby:C:\\Users\\Anton\\Desktop\\Imperial Camp\\PandemicDB;create=true; user = pandemic; password = pandemic");

			PreparedStatement selectFromDisease = con
					.prepareStatement("select * from factoryData.cities");
			ResultSet res = selectFromDisease.executeQuery();

			while (res.next()) {
				String name = res.getString("name");
				String diseaseName = res.getString("diseaseName");
				int topDistanse = res.getInt("topDistanse");
				int leftDistanse = res.getInt("leftDistanse");

				cities.put(name, new City(name, diseases.get(diseaseName),
						topDistanse, leftDistanse));
			}

		} catch (InstantiationException | IllegalAccessException
				| ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return cities;
	}

	public static InfectionDeck createInfectionDeck() {

		LinkedList<ICard> infectionDeck = new LinkedList<ICard>();

		for (City city : cities.values()) {
			infectionDeck.add(new InfectionCityCard(city));
		}

		return new InfectionDeck(infectionDeck);

	}

	public static PlayerDeck createPlayerDeck() {

		LinkedList<ICard> playerDeck = new LinkedList<ICard>();

		for (City city : cities.values()) {
			playerDeck.add(new PlayerCityCard(city));
		}

		return new PlayerDeck(playerDeck);

	}

}
